package com.taotao.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @Description: 零拷贝，NIO客户端
 * @Author: TaoJF
 * @Date: Created in 2018/7/8 下午3:41
 */
public class NewIOClient {

    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost", 8088));
        socketChannel.configureBlocking(true);

        String fileName = "/Users/taotao/Downloads/VMware Fusion Pro8.rar";

        FileChannel fileChannel = new FileInputStream(fileName).getChannel();

        long startTime = System.currentTimeMillis();

        long transferCount = fileChannel.transferTo(0, fileChannel.size(), socketChannel);

        System.out.printf("发送总字节数：%d，耗时：%dms%n", transferCount, System.currentTimeMillis() - startTime);

        fileChannel.close();
        socketChannel.close();
    }

}
